﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Common.Entities;

namespace Common.Models
{
    public class OrderRecordRepository
    {
        public List<OrderRecord> GetOrderRecordByOrder(int orderId)
        {
            try
            {
                List<OrderRecord> records = new List<OrderRecord>();
                using (SqlCeConnection connection = WorkDataBase.CreateConnection())
                {
                    if (connection.State != ConnectionState.Open)
                        connection.Open();
                    string sqlQuery =
                        "SELECT IdSaleGood, Quantity, Price, GoodId " +
                        "FROM SalesGoods " +
                        "Where OrderId = @OrderId";
                    using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
                    {
                        com.Parameters.Add("@OrderId", orderId);

                        using (SqlCeDataReader dr = com.ExecuteReader())
                        {
                            int idSales = dr.GetOrdinal("IdSaleGood");
                            int quantity = dr.GetOrdinal("Quantity");
                            int price = dr.GetOrdinal("Price");
                            int goodId = dr.GetOrdinal("GoodId");
                            while (dr.Read())
                            {
                                OrderRecord orderRecord = new OrderRecord();
                                orderRecord.IdOrderRecord = dr.GetInt32(idSales);
                                orderRecord.Price = dr.GetDouble(price);
                                orderRecord.Quantity = dr.GetDouble(quantity);
                                orderRecord.Amount = orderRecord.Quantity * orderRecord.Price;
                                orderRecord.Good = DataManager.GetInstance().Goods.GetGoodById(dr.GetInt32(goodId));
                                records.Add(orderRecord);
                            }
                            return records;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteOrderRecord(int orderRecordId)
        {
            try
            {
                using (SqlCeConnection connection = WorkDataBase.CreateConnection())
                {
                    if (connection.State != ConnectionState.Open)
                        connection.Open();
                    string sqlQuery =
                        "delete from SalesGoods where IdSaleGood = @IdSalesGood";
                    using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
                    {

                        com.Parameters.Add("@IdSalesGood", orderRecordId);
                        com.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void InsertOrderRecord(OrderRecord orderRecord, int orderId)
        {
            try
            {
                using (SqlCeConnection connection = WorkDataBase.CreateConnection())
                {
                    if (connection.State != ConnectionState.Open)
                        connection.Open();
                    string sqlQuery =
                        "insert into SalesGoods (OrderId, GoodId, Price, Quantity) values (@OrderId, @GoodId, @Price, @Quantity)";
                    using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
                    {
                        com.Parameters.Add("@OrderId", orderId);
                        com.Parameters.Add("@GoodId", orderRecord.Good.idGood);
                        com.Parameters.Add("@Price", orderRecord.Price);
                        com.Parameters.Add("@Quantity", orderRecord.Quantity);
                        com.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
